//
//  UIViewController+tool.m
//  SideDoctor
//
//  Created by 上官惠阳 on 15/9/18.
//  Copyright (c) 2015年 上官惠阳. All rights reserved.
//

#import "UIViewController+tool.h"
#import "HYJudgeOK.h"
#import "MLInputDodger.h"

@implementation UIViewController (tool)
//相机或相册页面
-(void)createImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
    if (sourceType == UIImagePickerControllerSourceTypeCamera && ![HYJudgeOK cameraOK]) {
        return ;
    }
    if ((sourceType == UIImagePickerControllerSourceTypePhotoLibrary
         || sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum)
        && ![HYJudgeOK AlbumOK]) {
        return ;
    }
    // 跳转到相机或相册页面
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    
    imagePickerController.delegate = self;
    
    imagePickerController.allowsEditing = YES;
    
    imagePickerController.sourceType = sourceType;
    
    //导航栏背景颜色
    imagePickerController.navigationBar.barTintColor = CJNavColor;
    //导航栏标题颜色
    [imagePickerController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    //导航栏按钮颜色
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
    [self presentViewController:imagePickerController animated:YES completion:^{}];
}
//从相册或相机上传图片
-(void)updateImageFromCameraOrPhotoLibrary
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
            
            [self createImagePickerControllerWithSourceType:UIImagePickerControllerSourceTypeCamera];
        }];
        [alertController addAction:cameraAction];
    }
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {

        [self createImagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) {
        
    }];
    [alertController addAction:photoAction];
    [alertController addAction:cancleAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
//躲避键盘
- (void)makeInputDodger
{
    //距离键盘距离(躲避键盘)
    self.view.shiftHeightAsDodgeViewForMLInputDodger = 50;
    //隐藏右上角的小键盘按钮
    self.view.dontUseDefaultRetractViewAsDodgeViewForMLInputDodger = YES;
    //注册
    [self.view registerAsDodgeViewForMLInputDodger];
}
@end
